package com.magnificent.panda.ui.editor;

import com.silverhaze.commons.ui.dialogs.DialogFactory;
import com.magnificent.graph.model.Cell;
import com.magnificent.graph.model.CellRole;
import com.magnificent.panda.model.entity.FlowActor;
import com.magnificent.panda.model.entity.FlowActorType;
import com.magnificent.panda.resources.Resources;
import com.magnificent.panda.ui.dialog.ActorSettingsDialog;
import com.magnificent.panda.ui.dialog.CorrelationSettingsDialog;
import com.magnificent.panda.ui.editor.EditorActions.HistoryAction;
import com.magnificent.panda.ui.util.mxGraphActions;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EditorPopupMenu extends JPopupMenu {
    private static final long serialVersionUID = -3132749140550242191L;

    public EditorPopupMenu(final BasicGraphEditor editor, Object objCell) {
        boolean selected = !editor.getGraphComponent().getGraph()
                .isSelectionEmpty();

        if (objCell != null) {
            final Cell cell = (Cell) objCell;

            add(editor.bind(cell.getRole() == CellRole.ACTOR ? "<html><b>Настройки</b></html>" : "<html><b>Моделирование</b></html>", new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    if (cell.getRole().equals(CellRole.ACTOR)) {
                        ActorSettingsDialog.show(cell);
                    } else if (cell.getRole().equals(CellRole.CORRELATION_FUNCTION)) {
                        if (cell.getEdgeCount() == 2) {
                            Cell fisrtActor = (Cell) cell.getEdgeAt(0).getTerminal(true);
                            Cell secondActor = (Cell) cell.getEdgeAt(1).getTerminal(true);

                            CorrelationSettingsDialog.show(fisrtActor, Collections.singletonList(secondActor), cell);
                        } else {
                            Cell mainCell = null;
                            List<Cell> slaveCells = new ArrayList<Cell>();

                            for (int i = 0; i < cell.getEdgeCount(); i++) {
                                Cell temp = (Cell) cell.getEdgeAt(i).getTerminal(true);
                                FlowActor actor = temp.getFlowActor();

                                if (actor != null) {
                                    if (actor.getActorType().equals(FlowActorType.MASTER)) {
                                        mainCell = temp;
                                    } else {
                                        slaveCells.add(temp);
                                    }
                                }
                            }

                            if (mainCell != null) {
                                CorrelationSettingsDialog.show(mainCell, slaveCells, cell);
                            } else {
                                DialogFactory.error("Ошибка. Не определен гланый пользователь.", null);
                            }
                        }
                    }
                }
            }, cell.getRole() == CellRole.ACTOR ? Resources.SETTINGS_ICON : Resources.MODELING_ICON));

//        add(editor.bind("Редактирование", mxGraphActions.getEditAction(),
//                "/com/magnificent/panda/ui/images/edit.png"));

            addSeparator();
        }

        add(
                editor.bind("Вырезать", TransferHandler
                        .getCutAction(),
                        "/com/magnificent/panda/ui/images/cut.gif"))
                .setEnabled(selected);
        add(
                editor.bind("Копировать", TransferHandler
                        .getCopyAction(),
                        "/com/magnificent/panda/ui/images/copy.gif"))
                .setEnabled(selected);
        add(editor.bind("Вставить", TransferHandler
                .getPasteAction(),
                "/com/magnificent/panda/ui/images/paste.gif"));

        addSeparator();

        add(editor.bind("Отмена", new HistoryAction(true),
                "/com/magnificent/panda/ui/images/undo.gif"));

        add(
                editor.bind("Удалить", mxGraphActions
                        .getDeleteAction(),
                        "/com/magnificent/panda/ui/images/delete.gif"))
                .setEnabled(selected);

        addSeparator();

        // Creates the format menu
        // TODO MENU "FORMAT"
//        JMenu menu = (JMenu) add(new JMenu(mxResources.get("format")));
//
//        EditorMenuBar.populateFormatMenu(menu, editor);
//
        // Creates the shape menu
//        menu = (JMenu) add(new JMenu(mxResources.get("shape")));
//
//        EditorMenuBar.populateShapeMenu(menu, editor);
//
//        addSeparator();
//
//        add(
//                editor.bind(mxResources.get("edit"), mxGraphActions
//                        .getEditAction())).setEnabled(selected);
//
//        addSeparator();
//
//        add(editor.bind(mxResources.get("selectVertices"), mxGraphActions
//                .getSelectVerticesAction()));
//        add(editor.bind(mxResources.get("selectEdges"), mxGraphActions
//                .getSelectEdgesAction()));
//
//        addSeparator();

        add(editor.bind("Выделить всё", mxGraphActions
                .getSelectAllAction(), "/com/magnificent/panda/ui/images/selectall.png"));
    }

}

  /* converted to utf8 */